<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * CMS Settings
     */
    class Cmssettings extends CI_Controller {

        private $data;

        function __construct() {

            parent::__construct();

            if (!$this->session->userdata('logged_in'))
                redirect(base_url('login'));

            $this->load->config('languages');

            $this->load->model('settings_model');
        }

        public function index() {
            if (!isset($_GET['post_type'])) {
                $_GET['post_type'] = "page";
            }
            $this->view_all();
        }

        function submit_post() {

            $message = "Something went wrong. Please try again.";

            switch ($_POST['post_action']) {

                case 'NEW': $this->settings_model->create_thumbnail_size();
                    $message = 'Successful new operation';
                    break;

                case 'EDIT': $this->settings_model->update_thumbnail_size();
                    $message = 'Successful edit operation';
                    break;
            }

            echo json_encode(array('action' => true));
        }

        function view_all_thimbnail_size() {


            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "View All " . $_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type'];
            $this->data['all_posts'] = $this->settings_model->view_all_thumbnail_size();

            $this->template->load('cmssettings/view_all_thumbnail_size', $this->data);
        }

        function view_edit_thimbnail_size() {

            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "Edit " . $_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type'];
            $this->data['post_action'] = 'EDIT';
            $this->data['post'] = $this->settings_model->view_edit_thimbnail_size($_GET['post_ID']);
            $this->template->load('cmssettings/view_edit_thumbnail_size', $this->data);
        }

        function view_add_thimbnail_size() {

            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "Add " . $_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type'];
            $this->data['post_action'] = 'NEW';
            $this->template->load('cmssettings/view_edit_thumbnail_size', $this->data);
        }

        // Create thumbnails again from dim in config file
        function create_thumbnails_again() {

            ini_set('max_execution_time', 5000); //300 seconds = 5 minutes
            ini_set('memory_limit', '254M'); 


            $this->load->model('gallery_model');
            $result = $this->gallery_model->create_thumbnails_again();

            if ($result)
                redirect(base_url('cmssettings/view_all_thimbnail_size?post_type=thumbnails'));
            else
                echo json_encode(array('action' => true, 'msg' => 'Something went wrong. Please try again.'));
        }

        function delete_thumbnail() {

            $this->settings_model->delete_thumbnail($_GET['post_ID']);

            redirect(base_url('cmssettings/view_all_thimbnail_size?post_type=thumbnails'));
        }

    }